/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.util.impl;

import es.silenus.detecta.util.PasswordGenerator;

import java.util.Random;


/**
 * Password encoder.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 25-mar-2009 13:10:53
 */
public class PasswordGeneratorImpl implements PasswordGenerator {
	/**
	 * Minimum length for a decent password
	 */
	public static final int MIN_LENGTH = 10;

	/**
	 * Set of characters that are valid.
	 */
	private static final char[] CHARS = {
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
			'2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '@',
	};

	/**
	 * The random number generator.
	 */
	private final Random random;

	/**
	 * Constructor.
	 */
	public PasswordGeneratorImpl() {
		random = new Random();
	}

	/**
	 * Generates a password
	 *
	 * @param maxLength the maximum length.
	 *
	 * @return the generated password.
	 */
	public String generatePassword(int maxLength) {
		StringBuffer sb = new StringBuffer();

		for(int i = 0; i < maxLength; i++) {
			sb.append(CHARS[random.nextInt(CHARS.length)]);
		}

		return sb.toString();
	}
}
